package com.tanhua.api.mongo;

import com.baomidou.mybatisplus.extension.api.R;
import com.tanhua.domain.mongo.BaoGao;
import com.tanhua.domain.mongo.Options;
import com.tanhua.domain.mongo.Questions;
import com.tanhua.domain.mongo.Soul;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

@Service
public class SoulApiImpl implements SoulApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public List<Soul> findSoul() {

        return mongoTemplate.findAll(Soul.class, "soul_test");
    }

    @Override
    public int findSoulQuestionCore(String questionId, String optionId) {
        Query newQuery=new Query(Criteria.where("id").is(new ObjectId(questionId)));
        Questions questions = mongoTemplate.findOne(newQuery, Questions.class,"soul_question");
        List<Options> optionsList = questions.getOptions();
        Options options = new Options();
        for (Options option : optionsList) {
            if (option.getId().equals(new ObjectId(optionId))){
                options = option;
            }
        }
        String option = options.getOption().split("、")[0];
        switch (option){
            case "A":
                return 7;
            case "B":
                return 5;
            case "C":
                return 3;
            case "D":
                return 4;
            case "E":
                return 2;
            case "F":
                return 1;
            case "G":
                return 0;
        }
        return 0;
    }

    @Override
    public void addBaoGao(BaoGao baoGao) {
        mongoTemplate.save(baoGao,"soul_baogao");
    }

    @Override
    public BaoGao report(ObjectId id) {
        BaoGao baoGao = mongoTemplate.findById(id, BaoGao.class,"soul_baogao");
//        Update update = new Update();
//        update.set("reportId",baoGao.getId());
//        Query query = new Query(Criteria.where("id").is(baoGao.getId()));
//        mongoTemplate.updateFirst(query,update,Soul.class,"soul_test");
        return baoGao;
    }

    @Override
    public void setReportId(String questionId, BaoGao baoGao) {
        Questions questions = mongoTemplate.findById(questionId, Questions.class, "soul_question");//查到问题
        Soul soul = mongoTemplate.findById(questions.getSoulId(), Soul.class, "soul_test");//查到问题对应的试卷
        Query query = new Query(Criteria.where("id").is(soul.getId()));
        Update update = new Update();
        update.set("reportId",baoGao.getId());
        mongoTemplate.updateFirst(query,update,Soul.class,"soul_test");//把试卷里的结果id赋值
        switch (soul.getLevel()){//判断下一等级并解锁
            case "初级":{
                Query newQuery = new Query(Criteria.where("level").is("中级"));
                Update newUpdate = new Update();
                newUpdate.set("isLock",0);
                mongoTemplate.updateFirst(newQuery,newUpdate,Soul.class,"soul_test");
                break;
            }
            case "中级":{
                Query newQuery = new Query(Criteria.where("level").is("高级"));
                Update newUpdate = new Update();
                newUpdate.set("isLock",0);
                mongoTemplate.updateFirst(newQuery,newUpdate,Soul.class,"soul_test");
                break;
            }
        }

    }

}
